package com.sweet.util;

import com.sweet.lang.StringUtil;

/**
 * 断言工具，仿自org.springframework.util.Assert
 *
 * @author limi
 * @version 2015-10-29
 */
public final class Assert {
    private Assert() {
    }

    /**
     * 断言object不为null
     *
     * @param object
     * @param message
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 断言字符串不为null或空
     *
     * @param str
     * @param message
     */
    public static void notEmpty(String str, String message) {
        if (StringUtil.isEmpty(str)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 断言字符串不为null或空白字符串
     *
     * @param str
     * @param message
     */
    public static void notBlank(String str, String message) {
        if (StringUtil.isBlank(str)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 判断superClazz是否为subClazz的父类
     *
     * @param superType 父类或接口
     * @param subType   子类或实现类
     * @return
     */
    public static void isAssignable(Class<?> superType, Class<?> subType) {
        isAssignable(superType, subType, null);
    }

    /**
     * 判断superClazz是否为subClazz的父类
     *
     * @param superType 父类或接口
     * @param subType   子类或实现类
     * @param message   异常信息
     * @return
     */
    public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
        notNull(superType, "Super type to check against must not be null");

        if (subType == null || !superType.isAssignableFrom(subType)) {
            throw new IllegalArgumentException((message != null ? message + " " : "") + subType + " is not assignable to " + superType);
        }
    }
}
